import {
  curTimeAll
} from '../../third-utils/time/index.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showModal:false,
        showOther:false,
        recordDate:'',
        categroyOther:[],
        categroyList:[],
        curCate:0,
        gmy_cftj:{},
        cftjIndex:'',
        selectYs:-1,
        ysgmAttr:[],
        firstDate:'',
        imageAttr:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(e) {
      this.getCurData(13,12)
        wx.request({
            url: 'https://babyplanet.isv-dev.youzan.com/api/recordType/findList',
            data: {
                parentId:e.cateId
            },
            method: 'post',
            success: (res) => {
              if (res.data.code == 200) {
                this.setData({
                    categroyList:res.data.data,
                    recordDate:curTimeAll
                })
              }
            }
          })

          wx.request({
            url: 'https://babyplanet.isv-dev.youzan.com/api/recordType/findList',
            data: {},
            method: 'post',
            success: (res) => {
              if (res.data.code == 200) {
                this.setData({
                  categroyOther:res.data.data
                })
              }
            }
          })
          
          this.initData()
    },
    selectItem(e){
      let idx=e.currentTarget.dataset.index
      this.setData({
        selectYs:this.data.selectYs==idx?-1:idx
      })
      this.setData({
        cftjIndex:''
      })
      let info=''
      if(this.data.selectYs>=0){
        console.log(this.data.ysgmAttr,1221)
        info=this.data.ysgmAttr[this.data.selectYs].detail

        if(info.triggerConditions){
          for(let i in this.data.gmy_cftj.opts){
              if(this.data.gmy_cftj.opts[i].name==info.triggerConditions){
                this.setData({
                  cftjIndex:i
                })
              }
          }
      }
      }
      console.log(info,1221)
      this.setData({
        allergen:this.data.selectYs>=0?info.allergen:'',
        firstDate:this.data.selectYs>=0?info.recordDate:'',
        allergenProduct:this.data.selectYs>=0?info.allergenProduct:'',
        description:this.data.selectYs>=0?info.description:'',
      })

      
    },
    bindDateChange(e){
      console.log(e,11221)
      this.setData({
        firstDate:e.detail.value
      })
    },
    getCurData(ysTypeId,yqdTypeId){
        
      wx.request({
        url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/getRecordForYSGMY',
        data: {
          babyId:34,
          ysTypeId:ysTypeId,
          yqdTypeId:yqdTypeId
        },
        method: 'post',
        success: (res) => {
          if (res.data.code == 200) {
              if(res.data.data.length>0){
                  this.setData({
                    ysgmAttr:res.data.data
                })
              }
          }
        }
      })
    },
    slider2change(){},
    select(e){
      this.setData({
        [e.currentTarget.dataset.key]:e.currentTarget.dataset.index
      })
    },
    toAdd(e){
      let idx=e.currentTarget.dataset.index
      let route=[
        {
          title:'消化道',
          path:'/pages/event-xhd/index?cateId='
        }, {
          title:'皮肤',
          path:'/pages/event-pf/index?cateId='
        }, {
          title:'呼吸道',
          path:'/pages/event-hxd/index?cateId='
        }
      ]
      route.some(item=>{
        if(item.title==this.data.categroyOther[idx].name){

          wx.redirectTo({
            url: item.path+this.data.categroyOther[idx].id
          })
        }
      })
    },
  formSubmit(e){
    console.log(e,12122)
    if(!e.detail.value.allergen){
      let firstTips=this.data.curCate==0?"过敏原":'疑似过敏原'
      wx.showToast({
        title: '请填写'+firstTips,
        icon: 'none',
        duration: 1500 //持续的时间
      })
      return
    }
    if(this.data.cftjIndex==''){
      wx.showToast({
        title: '请选择过敏触发条件',
        icon: 'none',
        duration: 1500 //持续的时间
      })
      return
    }
    let post={
      babyId:34,
      typeId:this.data.categroyList[this.data.curCate].id,
      
    }
      post.detail={
        recordDate:this.data.recordDate,
        triggerConditions:this.data.gmy_cftj.opts[this.data.cftjIndex].name,
        allergen:e.detail.value.allergen,
        allergenProduct:e.detail.value.allergenProduct,
        description:e.detail.value.description,
        picture:String(this.data.imageAttr)
      }
      if(this.data.curCate==0){
        post.detail.firstDate=this.data.firstDate
      }
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/add',
      data:post,
      method: 'post',
      success:  (res)=> {
        console.log(res,122)
        if (res.data.code == 200) {
          
          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          if(this.data.cateId==1){
            this.setData({
              showModal:true
            })
          }else{
            setTimeout(()=>{
              wx.navigateBack()
            },1500)

          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
  toNo(){
    this.hideModal()
    wx.navigateBack()
  },
  toSave(){
    this.setData({
      showOther:true
    })
    
  },
  hideModal(){
    this.setData({
      showModal:false
    })
  },
    pickerChangeType(e){
      console.log(e,1111)
      this.setData({
        cftjIndex:e.detail.value
      })
    },
    changeCate(e){
      this.setData({
        curCate:e.currentTarget.dataset.index
      })
    },
    delImg(e){
      this.data.imageAttr.splice(e.currentTarget.dataset.index,1)
      this.setData({
        imageAttr:this.data.imageAttr
      })
    },
    addImg(){
      wx.chooseImage({
        count:9,
        success: (chooseImageRes) => {
          console.log(chooseImageRes)
          const tempFilePaths = chooseImageRes.tempFilePaths;
          tempFilePaths.some((item)=>{
            wx.uploadFile({
              url: 'https://babyplanet.isv-dev.youzan.com/common/image/upload', //仅为示例，非真实的接口地址
              filePath:item,
              fileType: 'image',
              name: 'file',
              headers: {
                'Accept': 'application/json',
                'Content-Type': 'multipart/form-data',
              },
              success: (uploadFileRes) => {
                let res=JSON.parse(uploadFileRes.data)
                if(res.code==200){
                  this.data.imageAttr.push(res.data.imageUrl)
                  this.setData({
                    imageAttr:this.data.imageAttr
                  })
                }
              },
            });
          })
        }
      });
    },
    initData(){
        let codeAttr=['gmy_cftj']
        codeAttr.some(item=>{
            wx.request({
                url: 'https://babyplanet.isv-dev.youzan.com/api/selectConfig/view',
                data: {
                    code:item
                },
                method: 'get',
                success: (res) => {
                  if (res.data.code == 200) {
                    this.setData({
                        [item]:res.data.data
                    })
                  }
                }
              })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})